package com.block.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.block.common.model.dto.NewMemberDTO;
import com.block.common.model.vo.SourceDictVO;
import com.block.common.service.IMemberCommonService;
import com.block.db.entity.MemberBaseInfo;
import com.block.db.entity.MemberSourceDictInfo;
import com.block.db.mapper.MemberBaseInfoMapper;
import com.block.db.mapper.MemberSourceDictInfoMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class MemberCommonService implements IMemberCommonService {

    private static Integer level_1 = Integer.valueOf(0);

    private String SOURCE_DICT_INFO = "SOURCE:DICT:INFO:";
    private String SOURCE_SINGLE_DICT_INFO = "SOURCE:SINGLE:DICT:INFO:";
    private  static  final  Long SOURCE_DICT_INFO_TIME_OUT=4L;

    @Resource
    private RedisTemplate<String, List<SourceDictVO>> redisSourceDictVOListTemplate;
    @Resource
    private RedisTemplate<String, SourceDictVO> redisSourceDictVOTemplate;
    @Resource
    MemberBaseInfoMapper memberBaseInfoMapper;
    @Resource
    MemberSourceDictInfoMapper memberSourceDictInfoMapper;

    @Override
    public MemberBaseInfo saveNewMemberInfo(NewMemberDTO newMemberDTO) {

        MemberBaseInfo memberBaseInfo = new MemberBaseInfo();
        memberBaseInfo.setDateInfo(LocalDate.now().toString());
        memberBaseInfo.setMobile(newMemberDTO.getMobile());
        memberBaseInfo.setMobileMd5(SecureUtil.md5(newMemberDTO.getMobile()).toLowerCase());
        memberBaseInfo.setUsername(DesensitizedUtil.mobilePhone(newMemberDTO.getMobile()));

        memberBaseInfo.setType(newMemberDTO.getType());
        memberBaseInfo.setRegType(newMemberDTO.getRegType());

        memberBaseInfo.setChannelCode(newMemberDTO.getChannelCode());
        memberBaseInfo.setChannelName(newMemberDTO.getChannelName());

        memberBaseInfo.setRegReqIp(newMemberDTO.getRegReqIP());
        memberBaseInfo.setRegAppCode(newMemberDTO.getRegAppCode());

        // 用户授信额度


        int insert = memberBaseInfoMapper.insert(memberBaseInfo);
        if (insert > 0){
            return memberBaseInfoMapper.selectById(memberBaseInfo.getUid());
        }

        // TODO: 2023/8/7 异常通知

        return null;
    }

    @Override
    public List<SourceDictVO> getDictDataInfo(){
        List<SourceDictVO> sourceDictVOList = this.queryCacheDictInfo(level_1);
        for (SourceDictVO sourceDictVO : sourceDictVOList) {
            List<SourceDictVO> itemInfoList = this.queryCacheDictInfo(sourceDictVO.getId());
            sourceDictVO.setItemInfoList(itemInfoList);
        }
        return sourceDictVOList;
    }

    public  List<SourceDictVO> queryCacheDictInfo(Integer parentId){

        String redisKey = SOURCE_DICT_INFO+parentId;
        List<SourceDictVO> sourceDictVOList = redisSourceDictVOListTemplate.opsForValue().get(redisKey);
        if(CollUtil.isEmpty(sourceDictVOList)){
            List<MemberSourceDictInfo> resultList = memberSourceDictInfoMapper.getDictInfoByParentId(parentId);

            if(CollUtil.isNotEmpty(resultList)) {
                sourceDictVOList = new ArrayList<>();
                for (MemberSourceDictInfo memberSourceDictInfo : resultList) {
                    SourceDictVO sourceDictVO = new SourceDictVO();
                    sourceDictVO.setId(memberSourceDictInfo.getId());
                    sourceDictVO.setName(memberSourceDictInfo.getName());
                    sourceDictVO.setValue(memberSourceDictInfo.getValue());
                    sourceDictVO.setFiledName(memberSourceDictInfo.getFiledName());
                    sourceDictVO.setSortWeight(memberSourceDictInfo.getSortWeight());
                    sourceDictVO.setParentId(memberSourceDictInfo.getParentId());

                    sourceDictVOList.add(sourceDictVO);
                }
                redisSourceDictVOListTemplate.opsForValue().set(redisKey, sourceDictVOList, SOURCE_DICT_INFO_TIME_OUT, TimeUnit.HOURS);
            }
        }
        return sourceDictVOList;
    }

    @Override
    public  SourceDictVO queryDictInfoByValue(String dictValue){
        String redisKey = SOURCE_SINGLE_DICT_INFO+dictValue;
        SourceDictVO sourceDictVO = redisSourceDictVOTemplate.opsForValue().get(redisKey);
        if(ObjectUtil.isEmpty(sourceDictVO)){
            MemberSourceDictInfo sourceDictInfo = memberSourceDictInfoMapper.getDictInfoByValue(dictValue);
            if (ObjectUtil.isNotEmpty(sourceDictInfo)){
                sourceDictVO = new SourceDictVO();
                sourceDictVO.setId(sourceDictInfo.getId());
                sourceDictVO.setName(sourceDictInfo.getName());
                sourceDictVO.setValue(sourceDictInfo.getValue());
                sourceDictVO.setFiledName(sourceDictInfo.getFiledName());
                sourceDictVO.setSortWeight(sourceDictInfo.getSortWeight());
                sourceDictVO.setParentId(sourceDictInfo.getParentId());
                sourceDictVO.setExtValue(sourceDictInfo.getExtValue());

                redisSourceDictVOTemplate.opsForValue().set(redisKey, sourceDictVO, SOURCE_DICT_INFO_TIME_OUT, TimeUnit.HOURS);
            }else{
                sourceDictVO = new SourceDictVO();
            }
        }
        return sourceDictVO;
    }

}
